package com.yixin.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.Dept;
import com.yixin.hospital.entity.domain.Dictionary;
import com.yixin.hospital.entity.dto.req.dictionary.DictionaryAddReqDTO;
import com.yixin.hospital.entity.dto.req.dictionary.DictionaryEditReqDTO;
import com.yixin.hospital.service.DictionaryService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dictionary")
@Api(value = "字典管理", tags = "字典管理")
public class DictionaryController {

    @Resource
    DictionaryService dictionaryService;

    @GetMapping("/list")
    @ApiOperation(value = "字典列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="type",value="0", paramType = "query")
    })
    public ApiResult list(@RequestParam Integer type) {
        LambdaQueryWrapper<Dictionary> deptQueryWrapper = new LambdaQueryWrapper<>();
        deptQueryWrapper.eq(Dictionary::getType, type);
        deptQueryWrapper.orderByAsc(Dictionary::getSort);
        List<Dictionary> list = dictionaryService.list(deptQueryWrapper);
        return ApiResult.success(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加字典")
    public ApiResult add(@Valid @RequestBody DictionaryAddReqDTO jobAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        Dictionary dictionary = new Dictionary();
        dictionary.setType(jobAddReqDTO.getType());
        dictionary.setName(jobAddReqDTO.getName());
        dictionary.setCreatedBy(userName);
        dictionary.setUpdatedBy(userName);
        long sort = dictionaryService.count(new LambdaQueryWrapper<Dictionary>().eq(Dictionary::getType, jobAddReqDTO.getType()));
        dictionary.setSort((int)sort+1);
        dictionaryService.save(dictionary);
        return ApiResult.success();
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑字典")
    public ApiResult edit(@Valid @RequestBody DictionaryEditReqDTO jobEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        Dictionary dictionary = new Dictionary();
        dictionary.setId(jobEditReqDTO.getId());
        dictionary.setType(jobEditReqDTO.getType());
        dictionary.setName(jobEditReqDTO.getName());
        dictionary.setSort(jobEditReqDTO.getSort());
        dictionary.setCreatedBy(userName);
        dictionary.setUpdatedBy(userName);
        dictionaryService.updateById(dictionary);
        return ApiResult.success();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除字典")
    public ApiResult remove(@PathVariable int id) {
        dictionaryService.removeById(id);
        return ApiResult.success();
    }

}
